//
//  SettingView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/7/1.
//

import SwiftUI
import ToastUI

struct SettingView: View {
    @StateObject private var logout = LoginViewModel()
    @State private var showToast = false
    
    var version: String {
        Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown"
    }
    
    var buildNumber: String {
        Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "Unknown"
    }
    
    @EnvironmentObject var appState : AppState
    
    var body: some View{
        ZStack (alignment: .center){
            Color(hex: "#0A0A0A")
                .ignoresSafeArea()
            VStack {
                VStack(spacing: 0) {
                    // 隐私安全条目
                    HStack {
                        Text("隐私与安全")
                            .font(.system(size: 16))
                            .foregroundColor(.white)
                        Spacer()
                        Image("xiangyou")
                    }
                    .padding(20)
                    .onTapGesture {
                        appState.navigateTo(.privacy(id: (URL(string: "https://www.baidu.com")!)))
                    }
                    // 用户协议
                    HStack {
                        Text("用户协议")
                            .font(.system(size: 16))
                            .foregroundColor(.white)
                        Spacer()
                        Image("xiangyou")
                    }
                    .padding(20)
                    .onTapGesture {
                        appState.navigateTo(.agreement)
                    }
                    
                    // 意见反馈
                    HStack {
                        Text("意见反馈")
                            .font(.system(size: 16))
                            .foregroundColor(.white)
                        Spacer()
                        Image("xiangyou")
                    }
                    .padding(20)
                    .onTapGesture {
                        appState.navigateTo(.feedback)
                    }
                    
                    // 版本号
                    HStack {
                        Text("版本号")
                            .font(.system(size: 16))
                            .foregroundColor(.white)
                        Spacer()
                        Text("Ver "+version+"."+buildNumber)
                            .font(.system(size: 16))
                            .foregroundColor(.white)
                        Image("xiangyou")
                    }
                    .padding(20)
                    // 关于我们
                    HStack {
                        Text("关于我们")
                            .font(.system(size: 16))
                            .foregroundColor(.white)
                        Spacer()
                        Image("xiangyou")
                    }
                    .padding(20)
                    .onTapGesture {
                        appState.navigateTo(.abouturs)
                    }
                }
                .background(Color(hex: "#1E1E1E"))
                .cornerRadius(16)
                .padding(.horizontal, 12)
                .padding(.top,12)
                Spacer()
                VStack {
                    Button {
                        logout.logOut()
                    } label: {
                        HStack {
                            Spacer()
                            Text("退出登录")
                                .font(.system(size: 16))
                                .foregroundStyle(Color(hex: "#FFFFFF"))
                            Spacer()
                        }
                    }.frame(height: 44)
                }
                .background(Color(hex: "#1E1E1E"))
                .cornerRadius(22)
                .padding(.horizontal,12)
                .padding(.bottom,12)
                .toast(isPresented: $showToast) {
                    ShowMessageView(message: logout.error)
                }
                .onChange(of: logout.error) {
                    newValue in
                    if let errorMessage = newValue, !errorMessage.isEmpty {
                        showToast = true
                        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                            showToast = false
                            logout.error = nil
                        }
                    }
                }
                .onChange(of: logout.isLogoutSuccess) {isSuccess in
                    if isSuccess {
                        print("操作成功")
                        appState.logout()
                        logout.isLogoutSuccess = false
                    }
                }
                
            }
        }.customNavigationBar(title: "设置", backButtonAction: {
            appState.navigateBack()
        }, backgroundColor: UIColor(.clear))
    }
}


#Preview {
    SettingView()
}
